// 重点：1.左侧点击激活状态
<template>
  <el-container class="home-container">
    <el-header class="home-header">
      <div>
        <img src="../assets/logo.png" alt />
        <span>管理系统</span>
      </div>
      <el-button @click="exitlogin()" type="danger" round plain>退出</el-button>
    </el-header>
    <el-container>
      <!-- !!!!重点学习  控制侧边栏 -->
      <el-aside :width="iscollapse? '64px':'200px'" class="home-aside">
        <!-- 收回左侧页面按钮 -->
        <div class="toggle-button" @click="zhediecollapse()">||||</div>
        <!-- @open=""
        @close=""-->
        <!-- collapse  unique-opened router 重点-->
        <el-menu
          :default-active="activePath"
          class="el-menu-vertical-demo"
          background-color="#545c64"
          text-color="#fff"
          active-text-color="#409EFF"
          unique-opened
          :router="true"
          :collapse="iscollapse"
          :collapse-transition="false"
        >
          <!-- 左侧菜单 -->
          <el-submenu :index="'/'+item.path" v-for="item in menulist" :key="item.id">
            <template slot="title">
              <i class="el-icon-location"></i>
              <span>{{item.authName}}</span>
            </template>
            <el-menu-item-group >
              <!-- <template slot="title">分组</template> -->
              <el-menu-item @click="saveNavState('/'+itemin.path)" :index="'/'+itemin.path" :key="itemin.id" v-for="itemin in item.children" class="el-icon-menu"><span>{{itemin.authName}}</span></el-menu-item>
            </el-menu-item-group>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-container>
        <el-main class="home-main">
          <!-- 渲染处 -->
          <router-view></router-view>
        </el-main>
        <!-- <el-footer>Footer</el-footer> -->
      </el-container>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      menulist: [],
      picmenu: [], //里面可以写menulist的图标样式
      iscollapse: false,
      //被激活的左侧菜单栏
      activePath:null,
    }
  },
  created() {
    this.getMenuList() //获取menu左侧内容
    this.activePath=window.sessionStorage.getItem("activePath")//获得此时左侧应该打开的页面！
  },
  methods: {
    exitlogin() {
      //生命周期创建时就要获取菜单列表
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    //获取所有的菜单
    async getMenuList() {
      const { data: res } = await this.$http.get('menus')
      // console.log(res);
      if (res.meta.status !== 200) {
        return this.$message.error(res.meta.msg)
      } else {
        this.menulist = res.data
        // console.log(res.data)
      }
    },
    //点击切换折叠菜单
    zhediecollapse() {
      this.iscollapse = !this.iscollapse
    },
    //保存右侧打开的激活状态  点击获取一次，token中获取一次
    saveNavState(activePath){
      window.sessionStorage.setItem('activePath',activePath)
      this.activePath=activePath
    }
  }
}
</script>

<style lang="less" scoped>
.home-container {
  // background-color: saddlebrown;
  height: 100%;
}
.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  > div {
    display: flex;
    align-items: center;
    color: wheat;
    font-size: 100%;
    span {
      margin-left: 15px;
    }
  }
}
.el-aside {
  background-color: #333744;
  .el-menu {
    border-right: none;
  }
}
.toggle-button {
  background-color: #4a5064;
  font-size: 24px;
  line-height: 24px;
  color: #fff;
  text-align: center; //文字居中
  letter-spacing: 0.3em; //文字间间距
  cursor: pointer;
}
.el-menu-item{
  span{
    margin-left: 10px;
  }
}
</style>